package com.mpdbailey.cleverdicandroid;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;

import com.pigdogbay.androidutils.iab.IabHelper;
import com.pigdogbay.androidutils.iab.IabResult;
import com.pigdogbay.androidutils.iab.Inventory;
import com.pigdogbay.androidutils.iab.Purchase;
import com.pigdogbay.androidutils.utils.PreferencesHelper;

public class AppPurchases {
	public static final String SKU_TEST = "android.test.purchased";
	public static final String SKU_DISABLE_ADS = "com.pigdogbay.cleverdic.disable_ads";
	public static final String SKU_COLOR_PACK = "com.pigdogbay.cleverdic.color_pack";
	public static final String SKU_LARGE_DICTIONARY = "com.pigdogbay.cleverdic.large_dictionary";

	public static final String NEW_PURCHASE_INTENT = "com.mpdbailey.cleverdicandroid.NEW_PURCHASE_INTENT";
	public static final String EXTRA_PURCHASE_SKU = "EXTRA_PURCHASE_SKU";
	
	private static final String _base64EncodedPublicKey1 = "MIIBIjANBgkqhkiG";
	private static final String _base64EncodedPublicKey2 = "9w0BAQEFAAOCAQ8A";
	private static final String _base64EncodedPublicKey3 = "MIIBCgKCAQEAkpwK";
	private static final String _base64EncodedPublicKey4 = "o4vUDoSPAWGv+9uu";
	private static final String _base64EncodedPublicKey5 = "ubqUyk4FdfaaARBq";
	private static final String _base64EncodedPublicKey6 = "JKfWD3FxjGmJ7UE9";
	private static final String _base64EncodedPublicKey7 = "zwyRX2XIxJ0LXUs8";
	private static final String _base64EncodedPublicKey8 = "EFQsMuEZSumAjGRg";
	private static final String _base64EncodedPublicKey9 = "Fe/2r55pV9+HNEDm";
	private static final String _base64EncodedPublicKey10 = "CSq4ZDmVI6VgDxSS";
	private static final String _base64EncodedPublicKey11 = "9Yk8D96lNEPuWjsn";
	private static final String _base64EncodedPublicKey12 = "Wz8St1QudDZ3Tbzq";
	private static final String _base64EncodedPublicKey13 = "QnVvzT3/SALDJoBL";
	private static final String _base64EncodedPublicKey14 = "y7O4KXQynut5ZIcM";
	private static final String _base64EncodedPublicKey15 = "ZZhVXVp4up7dHNrs";
	private static final String _base64EncodedPublicKey16 = "pomuqV/itHpYdwOq";
	private static final String _base64EncodedPublicKey17 = "2/ROgWp1uvi+db85";
	private static final String _base64EncodedPublicKey18 = "AkatBVBwDvcMmAet";
	private static final String _base64EncodedPublicKey19 = "41y4CShsBPZlbcU+";
	private static final String _base64EncodedPublicKey20 = "3TkS1ewD8KgIGYzh";
	private static final String _base64EncodedPublicKey21 = "zAn4kSmFAvbVmfeG";
	private static final String _base64EncodedPublicKey22 = "4YLmBe0bw0vU6XhJ";
	private static final String _base64EncodedPublicKey23 = "KRxUHqnEa56Cu2S/";
	private static final String _base64EncodedPublicKey24 = "2YwK8zd08jZKSJ1z";
	private static final String _base64EncodedPublicKey25 = "YwIDAQAB";
	
	public static String getPublicKey() {
		StringBuffer sbuffer = new StringBuffer();
		sbuffer.append(_base64EncodedPublicKey1);
		sbuffer.append(_base64EncodedPublicKey2);
		sbuffer.append(_base64EncodedPublicKey3);
		sbuffer.append(_base64EncodedPublicKey4);
		sbuffer.append(_base64EncodedPublicKey5);
		sbuffer.append(_base64EncodedPublicKey6);
		sbuffer.append(_base64EncodedPublicKey7);
		sbuffer.append(_base64EncodedPublicKey8);
		sbuffer.append(_base64EncodedPublicKey9);
		sbuffer.append(_base64EncodedPublicKey10);
		sbuffer.append(_base64EncodedPublicKey11);
		sbuffer.append(_base64EncodedPublicKey12);
		sbuffer.append(_base64EncodedPublicKey13);
		sbuffer.append(_base64EncodedPublicKey14);
		sbuffer.append(_base64EncodedPublicKey15);
		sbuffer.append(_base64EncodedPublicKey16);
		sbuffer.append(_base64EncodedPublicKey17);
		sbuffer.append(_base64EncodedPublicKey18);
		sbuffer.append(_base64EncodedPublicKey19);
		sbuffer.append(_base64EncodedPublicKey20);
		sbuffer.append(_base64EncodedPublicKey21);
		sbuffer.append(_base64EncodedPublicKey22);
		sbuffer.append(_base64EncodedPublicKey23);
		sbuffer.append(_base64EncodedPublicKey24);
		sbuffer.append(_base64EncodedPublicKey25);
		return sbuffer.toString();
	}

	Context _Context;
	IabHelper _Helper;
	Inventory _Inventory = null;

	public AppPurchases(Context context) {
		_Context = context;
	}

	public void SetPreferences(Inventory inventory) {

		PreferencesHelper prefHelper = new PreferencesHelper(_Context);
		if (inventory.hasDetails(SKU_LARGE_DICTIONARY)){
			prefHelper.setBoolean(R.string.code_pref_unlock_large_dictionary_key,
					inventory.hasPurchase(SKU_LARGE_DICTIONARY));
		}
		if (inventory.hasDetails(SKU_DISABLE_ADS)) {
			prefHelper.setBoolean(R.string.code_pref_disable_ads_key,
					inventory.hasPurchase(SKU_DISABLE_ADS));
		}
		if (inventory.hasDetails(SKU_COLOR_PACK)){
			prefHelper.setBoolean(R.string.code_pref_unlock_color_pack_key,
					inventory.hasPurchase(SKU_COLOR_PACK));
		}
	}
	
	public static List<String> getAppSkus()
	{
		List<String> appSkus = new ArrayList<String>();
		appSkus.add(SKU_DISABLE_ADS);
		appSkus.add(SKU_COLOR_PACK);
		appSkus.add(SKU_LARGE_DICTIONARY);
		return appSkus;
	}

	public void QueryAsync() {
		_Helper = new IabHelper(_Context, getPublicKey());
		_Helper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
			public void onIabSetupFinished(IabResult result) {
				if (result.isSuccess()) {
					queryInventory(getAppSkus());
				}
			}
		});
	}

	/**
	 * @param appSkus null only returns purchased items
	 * 			need to pass in sku details to get details of non-purchased items
	 */
	private void queryInventory(List<String> appSkus) {
		_Helper.queryInventoryAsync(true, appSkus,
				new IabHelper.QueryInventoryFinishedListener() {
					@Override
					public void onQueryInventoryFinished(IabResult result,
							Inventory inv) {
						try {
							_Inventory = inv;
							if (result.isSuccess()) {
								SetPreferences(inv);
							}
						}
						finally {
							_Helper.dispose();
							_Helper = null;
						}
					}
				});

	}
	
	public void broadcastPurchase(String sku){
		Intent intent = new Intent(AppPurchases.NEW_PURCHASE_INTENT);
		intent.putExtra(AppPurchases.EXTRA_PURCHASE_SKU, sku);
		LocalBroadcastManager.getInstance(_Context).sendBroadcast(intent);
	}

}
